/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import SessionFactory.SessionFactoryUtil;
import beans.ViewTopicBean;
import client.TopicsPosts;
import client.ArrangePosts;
import dto.JfMembers;
import dto.Topics;
import dto.Posts;
import dto.Sections;
import dto.SectionsHome;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Amir
 */
public class ViewTopicAction extends org.apache.struts.action.Action {

    TopicsPosts posts = new TopicsPosts();

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = new ActionForward();
        Topics topic = posts.getTopicContents(new Integer(request.getParameter("topic")));
        ((ViewTopicBean) form).setTopicid(topic.getTopicid());
        ((ViewTopicBean) form).setTopicname(topic.getTopicname());
        ((ViewTopicBean) form).setCreationdate(topic.getCreationdate());
        ((ViewTopicBean) form).setPostses(topic.getPostses());
        ((ViewTopicBean) form).setReadmarks(topic.getReadmarks());
        //===============Check for readmark=======
        topic.setTopicid(topic.getTopicid());

       JfMembers member = (JfMembers)request.getSession().getAttribute("userBean");
       if(member!=null)
       {
        System.out.println(member.getMemberId()+"member");

       topic= posts.getTopic(topic);
       System.out.println(topic.getTopicid()+"topic");
        boolean flag=posts.getReadMark(topic,member);
           System.out.println(flag);
     // ((ViewTopicBean) form).setReadmark(flag);
           request.setAttribute("test", flag);
        }
        //========== Arrange Posts=======Amir=======

        Set<Posts> postses =topic.getPostses();


         Posts[] postes=new Posts[postses.size()];

           Iterator itr =postses.iterator();
           int i=0;
           while(itr.hasNext())
           {
            Posts po=new Posts();
            postes[i]=(Posts)itr.next();

            i++;
            }
            Comparator<Posts> sortById=new ArrangePosts();
            Arrays.sort(postes,sortById);
           
          ((ViewTopicBean) form).setPostes(postes);
        //==============
          SessionFactoryUtil.startNew();
        SectionsHome sectionsHome = new SectionsHome();
        ((ViewTopicBean) form).setAllSections(new Vector<Sections>(sectionsHome.getSections()));
        SessionFactoryUtil.finish();
        //=================== Prevent Spam =====================
        ((ViewTopicBean) form).setPreventSpamCheck(0);

        forward = mapping.findForward("done");
        return forward;

    }
}
